/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccessData;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author User
 */
public class SqlServer {

    private Connection getConnect() {
        Connection conn = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ManagerATM;user = sa; password = 123456");

        } catch (Exception e) {
            System.out.println("Loi " + e + " do " + e.getMessage());
        }
        return conn;
    }

    private Statement getStatement() {
        Statement stmt = null;
        try {
            stmt = getConnect().createStatement();
        } catch (Exception e) {
            System.out.println("Loi " + e + " do " + e.getMessage());
        }
        return stmt;
    }

    public ResultSet executeQuery(String Query) throws SQLException {
        return getStatement().executeQuery(Query);       
    }

    public int executeUpdate(String Query) throws SQLException {
        return getStatement().executeUpdate(Query);
    }

    public int executeProc(String spName, Vector paramList) throws SQLException {
        String strQ = "{call " + spName + "(";
        int t = 0;
        for (Object obj : paramList) {
            if (t != 0) {
                strQ += ",";
            }
            if (obj instanceof Integer) {
                Integer i = (Integer) obj;
                strQ += i.toString();
            } else if (obj instanceof String) {
                String s = (String) obj;
                strQ += "'" + s + "'";
            }
            t++;
        }
        strQ += ")}";
        CallableStatement csmt = getConnect().prepareCall(strQ);
        return csmt.executeUpdate();
    }
}
